<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 面试技巧 -->
      <el-card shadow="never">
        <!-- 筛选 -->
        <el-form :model="form" ref="form">
          <div class="filter-container">
            <el-row :gutter="4">
              <el-col :span="7">
                <el-form-item label="关键字" prop="keyword">
                  <el-input
                    style="width: 200px"
                    placeholder="根据文章标题搜索"
                    class="filter-item"
                    v-model="form.keyword"
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="状态：" prop="state">
                  <el-select
                    class="filter-item"
                    style="width: 200px"
                    v-model="form.state"
                    clearable
                  >
                    <el-option
                      v-for="item in status"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-button class="filter-item" size="small" @click="empty"
                  >清除</el-button
                >
                <el-button
                  class="filter-item"
                  size="small"
                  type="primary"
                  @click="search"
                  >搜索</el-button
                >
              </el-col>
              <el-col :span="3">
                <el-button
                  class="filter-item fr"
                  size="small"
                  style="margin-left: 10px"
                  type="success"
                  @click="addTicks"
                  ><i class="el-icon-edit"></i> 新增技巧</el-button
                >
              </el-col>
            </el-row>
          </div>
        </el-form>
        <!-- 信息总条数 -->
        <el-alert
          v-if="alertText !== ''"
          :title="alertText"
          type="info"
          class="alert"
          :closable="false"
          show-icon
        ></el-alert>
        <!-- 数据 -->
        <el-table
          :data="dataList"
          fit
          highlight-current-row
          style="width: 100%"
        >
          <el-table-column align="center" type="index" label="序号" />
          <el-table-column align="left" width="400px" label="文章标题">
            <template #default="scope">
              <div>
                {{ scope.row.title }}
                <el-button
                  type="text"
                  v-if="scope.row.videoURL"
                  @click="openView(scope.row)"
                  ><i class="el-icon-film"></i>
                </el-button>
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="visits" label="阅读数" />
          <el-table-column label="录入人" prop="username" />
          <el-table-column align="center" label="录入时间">
            <template #default="scope">
              <span>{{ formatDate(scope.row.createTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" label="状态">
            <template #default="scope">
              {{ formOfEmployment(scope.row.state) }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="操作" width="180px">
            <template #default="{ row }">
              <el-button type="text" size="small" @click="preArt(row)"
                >预览</el-button
              >
              <el-button type="text" size="small" @click="upState(row)">{{
                row.state === 1 ? "禁用" : "启用"
              }}</el-button>
              <el-button
                type="text"
                size="small"
                :disabled="row.state === 1"
                @click="upData(row)"
                >修改</el-button
              >
              <el-button
                type="text"
                size="small"
                @click="delItem(row)"
                :disabled="row.state === 1"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <!-- end -->
        <!-- 分页 -->
        <div class="pagination-container">
          <PageTool
            :paginationPage="params.page"
            :paginationPagesize="params.pagesize"
            :total="total"
            @pageChange="handleCurrentChange"
            @pageSizeChange="handleSizeChange"
          >
          </PageTool>
        </div>
        <!-- end -->
      </el-card>
    </div>
    <AddArticle
      :visible="visible"
      @update="getList"
      @closeDialog="closeDialog"
      :currentRow="currentRow"
    />
    <PreArticle
      :visiblePre="visiblePre"
      :currentRow="pretRow"
      @closeP="closeP"
    />
    <el-dialog
      title="视频展示"
      :visible.sync="showView"
      width="60%"
      @close="closeView"
    >
      <videoPlayer
        class="video-player vjs-custom-skin"
        ref="videoPlayer"
        :playsinline="true"
        :options="playerOptions"
      ></videoPlayer>
    </el-dialog>
  </div>
</template>

<script>
import { list, remove, changeState } from '@/api/hmmm/articles.js'
import PageTool from './../../module-manage/components/page-tool.vue'
import { status } from '@/api/hmmm/constants.js'
import AddArticle from './../components/articles-add'
import PreArticle from './../components/articles-preview.vue'
import dayjs from 'dayjs'
import { videoPlayer } from 'vue-video-player'
import 'video.js/dist/video-js.css'
export default {
  components: {
    PageTool, AddArticle, PreArticle, videoPlayer
  },
  data () {
    return {
      // 预览时当前行数据
      pretRow: {},
      // 当前数据行
      currentRow: {},
      // 视频弹层展示
      showView: false,
      // 预览展示
      visiblePre: false,
      // 弹层展示
      visible: false,
      // 搜素表单
      form: {
        keyword: '',
        state: ''
      },
      // str='默认值' str如果不传 读取默认值'YYYY-MM-DD'
      formatDate (value, str = 'YYYY-MM-DD hh:m:s') {
        return dayjs(value).format(str)
      },
      // 数据总和文本
      alertText: '',
      total: 0,
      // 分页数据
      params: {
        page: 1,
        pagesize: 10
      },
      // 数据列表
      dataList: [],
      // 枚举数据
      status,
      // 视频配置--------------------------------------------------
      playerOptions: {
        playbackRates: [0.7, 1.0, 1.5, 2.0], // 播放速度
        autoplay: true, // 如果true,浏览器准备好时开始播放。
        muted: false, // 默认情况下将会消除任何音频。
        loop: true, // 循环播放。
        preload: 'auto', // 建议浏览器在<video>加载元素后是否应该开始下载视频数据。auto浏览器选择最佳行为,立即开始加载视频（如果浏览器支持）
        language: 'zh-CN',
        aspectRatio: '16:9', // 将播放器置于流畅模式，并在计算播放器的动态大小时使用该值。值应该代表一个比例 - 用冒号分隔的两个数字（例如"16:9"或"4:3"）
        fluid: true, // 当true时，Video.js player将拥有流体大小。换句话说，它将按比例缩放以适应其容器。
        sources: [{
          src: 'https://v-cdn.zjol.com.cn/276984.mp4', // 路径
          type: 'video/mp4' // 类型
        }],
        poster: '../../static/images/test.jpg', // 你的封面地址
        // width: document.documentElement.clientWidth,
        notSupportedMessage: '此视频暂无法播放，请稍后再试', // 允许覆盖Video.js无法播放媒体源时显示的默认信息。
        controlBar: {
          timeDivider: true,
          durationDisplay: true,
          remainingTimeDisplay: false,
          fullscreenToggle: true // 全屏按钮
        }
      }
    }
  },
  created () {
    this.getList()
  },
  methods: {
    // 打开视频
    openView (row) {
      console.log(row.videoURL)
      this.playerOptions.sources[0].src = row.videoURL
      this.showView = true
    },
    // 关闭视频
    closeView () {
      this.playerOptions.sources[0].src = ''
      this.showView = false
    },
    // 关闭弹层
    closeDialog () {
      this.currentRow = {}
      this.visible = false
    },
    // 关闭预览弹层
    closeP () {
      this.pretRow = {}
      this.visiblePre = false
    },
    // 修改数据
    upData (row) {
      this.currentRow = row
      this.visible = true
    },
    // 状态修改
    async upState (row) {
      await changeState({ id: row.id, state: row.state === 1 ? 0 : 1 })
      this.$message.success('修改状态成功')
      this.getList()
    },
    // 打开预览弹窗
    preArt (row) {
      this.pretRow = row
      this.visiblePre = true
    },
    // 打开新增技巧弹层
    addTicks () {
      this.visible = true
    },
    // 删除
    async delItem (row) {
      console.log(row)
      this.$confirm('你确定删除吗', '温馨提示').then(async () => {
        await remove(row)
        this.$message.success('删除成功')
        if (this.total >= 1) this.params.page = Math.ceil((this.total - 1) / this.params.pagesize)
        this.getList()
      }
      ).catch(console.log)
    },
    // 格式化启用
    formOfEmployment (type) {
      const maps = {}
      status.forEach(item => {
        maps[item.value] = item.label
      })
      return maps[type]
    },
    // 清空
    empty () {
      this.form = {
        keyword: '',
        state: ''
      }
    },
    // 搜索
    async search () {
      this.getList()
    },
    // 初始化列表
    async getList () {
      const data = JSON.parse(JSON.stringify(this.params))
      if (this.form.keyword !== '') {
        data.keyword = this.form.keyword
      }
      if (this.form.state !== '') {
        data.state = this.form.state
      }
      const res = await list(data)
      const { counts, items } = res.data
      console.log(counts, items)
      this.total = counts
      this.alertText = `数据一共有${counts}条`
      this.dataList = items
    },
    // 每页显示信息条数
    handleSizeChange (val) {
      this.params.pagesize = val
      if (this.params.page === 1) {
        this.getList(this.params)
      }
    },
    // 进入某一页
    handleCurrentChange (val) {
      this.params.page = val
      this.getList()
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.alert {
  margin: 10px 0px;
}
.pagination {
  margin-top: 10px;
  text-align: right;
}
.el-icon-film {
  font-size: 20px;
}
.video-js .vjs-big-play-button {
}
</style>
